<template>
  <div class="goods">
    <!-- 搜索框 -->
    <div class="header">
      <!--change 仅在输入框失去焦点或用户按下回车时触发 -->
      <el-input
        @change="searchInp"
        v-model="input"
        placeholder="请输入内容"
      ></el-input>
      <el-button type="primary">查询</el-button>
      <el-button type="primary">
        <router-link to="/add-goods" style="color: #fff">添加商品</router-link>
      </el-button>
      <el-button type="primary" @click="addGoods">添加</el-button>
    </div>
    <!-- 表格视图区域 -->
    <div class="wrapper">
      <el-table :data="tableData" :border="false" style="width: 100%">
        <el-table-column type="selection" width="50"> </el-table-column>
        <el-table-column prop="id" label="商品ID" width="100">
        </el-table-column>
        <el-table-column
          prop="title"
          label="商品名称"
          width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="price" label="商品价格" width="100">
        </el-table-column>
        <el-table-column prop="num" label="商品数量" width="100">
        </el-table-column>
        <el-table-column prop="category" label="商品类目" width="100">
        </el-table-column>
        <el-table-column prop="image" label="商品图片" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="descs" label="商品描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button size="mini">查看</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <MyPagination
      @changePage="changePage"
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
    />
    <!-- 显示弹框组件 操作子组件:1.父传子  2.children  3.ref-->
    <!-- <GoodsDialog :dialogVisible="dialogVisible" @changeDialog="changeDialog"/>
     -->
    <GoodsDialog ref="dialog" :title="title" :rowData="rowData"/>
  </div>
</template>

<script>
import MyPagination from "@/components/MyPagination.vue";
import GoodsDialog from "./GoodsDialog.vue";

export default {
  data() {
    return {
      input: "",
      tableData: [],
      //数据接收为每页8个 总数33
      total: 10,
      pageSize: 1,
      type: 1,
      list: [],
      dialogVisible: false,
      currentPage: 1, //选中的高亮页码
      title:'添加商品',
      rowData:{}
    };
  },
  methods: {
    //添加商品出现弹框
    addGoods() {
      // this.dialogVisible = !this.dialogVisible
      //修改子组件的数据
      this.$refs.dialog.dialogVisible = true;
      this.title = '添加商品'
      this.$refs.dialog.goodsForm = []
    },
    changeDialog() {
      this.dialogVisible = false;
    },
    //分页页码
    changePage(num) {
      this.currentPage = num;
      if (this.type == 1) {
        this.http(num); //商品列表分页
      } else {
        //搜索分页 1 2 3 4 --  list=[0,1,2,3,4,5,6,7,8]  0-3  3-6  6-9 9-12
        console.log("搜索的分页处理---");
        //（num-1）*3  num*3
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    //搜索查询功能
    searchInp(val) {
      //如果搜索为空
      if (!val) {
        this.http(1);
        this.currentPage = 1;
        this.type = 1;
        return;
      }
      this.$api
        .getSearch({
          search: val,
        })
        .then((res) => {
          console.log(res.data);
          this.currentPage = 1;

          if (res.data.status === 200) {
            //请求成功将tableData 替换为 请求返回数据
            this.list = res.data.result; //获取的搜索数据的总条数  ---数据分割
            //这里我们假设请求回来的数据比较多，需要分页，设置分页  拿到result长度进行设置
            this.total = res.data.result.length;
            this.pageSize = 3;
            this.tableData = res.data.result.slice(0, 3);
            this.type = 2;
          } else {
            this.tableData = [];
            this.total = 1;
            this.pageSize = 1;
            this.type = 1;
          }
        });
    },
    //编辑操作
    handleEdit(index,row) {
      //1.点击编辑按钮后 显示弹框 注意是重新封装一个弹框组件还是共用弹框
      this.$refs.dialog.dialogVisible = true
      this.title = '编辑商品'//更改标题
      //2.弹框数据回显  展示当前行的数据
      // this.$refs.dialog.goodsForm = row
      this.rowData = {...row}
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //请求接口
          this.$api
            .getRemoveGoods({
              id: row.id,
            })
            .then((res) => {
              console.log(res.data);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //视图更新
              this.http(1)
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //商品列表的获取
    http(page) {
      this.$api
        .getGoodsList({
          page,
        })
        .then((res) => {
          if (res.data.status === 200) {
            this.tableData = res.data.data; //数据列表
            this.total = res.data.total;
            this.pageSize = res.data.pageSize;
            console.log(this.total);
          }
        });
    },
  },
  created() {
    this.http(1);
  },
  components: { MyPagination, GoodsDialog },
};
</script>

<style lang="less" scoped>
.goods {
  margin: 20px;
}
.header {
  display: flex;
  button {
    margin-left: 20px;
  }
}
.wrapper {
  margin: 20px 0;
}
</style>
